<?php

namespace Home\Controller;

use Common\Controller\HomeController;

class InventoryController extends HomeController
{
    /**
     * 库存盘点(货架整理)
     * @author Mr.Alien
     * @return string
     */
    public function index()
    {
        $this->where['user_id'] = session('id');
        $this->where['status'] = 2;
        I('get.no') && ($this->where['no'] = array('like','%'.I('get.no').'%'));
        $container_list  = D('Container')->where($this->where)->select();
        $this->assign('container_list',$container_list);
        $this->display();
    }
    
    /**
     * 获取货柜下快件
     * @author Mr.Alien
     * @return string
     */
    public function getPackage($id)
    {
        $list = D('Take')->relation(true)->where(array('container_id'=>(int)$id,'status'=>1))->select();
        $this->assign('list',$list);
        $this->display('getPackage');
    }

    /**
     * 获取待转入的货架
     * @author Mr.Alien
     * @param $id int
     * @return string
     */
    public function getContainer($id)
    {
        $list = D('Container')->where(array('status'=>2,'user_id'=>session('id'),'id'=>array('neq',(int)$id)))->select();
        $this->assign('list',$list);
        $this->display('getContainer');
    }

    /**
     * 保存需要转移的数据
     * @author Mr.Alien
     * @return string
     */
    public function save()
    {
        $data = I('post.');
        $accept = M('Container')->where(array('id'=>$data['accept_id']))->find();
        $send = M('Container')->where(array('id'=>$data['send_id']))->find();
        if(!$accept || !$send) $this->error('数据有误');
        if(count($data['package_ids']) > ($accept['capacity'] - $accept['used'])){
            $this->error('货柜余量不足');
        }
        $res = M('Take')->where(array('id'=>array('in',$data['package_ids'])))->save(array('container_id'=>$accept['id']));
        if($res === false){
            $this->error('转入失败');
        }else{
            M('Container')->where(array('id'=>$data['accept_id']))->setInc('used',count($data['package_ids']));
            M('Container')->where(array('id'=>$data['send_id']))->setDec('used',count($data['package_ids']));
            $this->success('转入成功');
        }
    }
}